package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.AlbumBackDTO;
import com.jyh.blog.dto.AlbumDTO;
import com.jyh.blog.service.AlbumService;
import com.jyh.blog.strategy.context.UploadStrategyContext;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.AlbumVO;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.REMOVE;
import static com.jyh.blog.constant.OptTypeConst.SAVE_OR_UPDATE;
import static com.jyh.blog.enums.FilePathEnum.PHOTO;

@Api(tags = "相册管理")
@RestController
public class AlbumController {
    @Autowired
    private AlbumService albumService;
    @Autowired
    private UploadStrategyContext uploadStrategyContext;

    /**
     * 获取后台相册列表
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "获取后台相册列表")
    @GetMapping("admin/album")
    public Result<PageResult<AlbumBackDTO>> albumBackList(ConditionVO conditionVO){
        //log.info("相册分页查询：{}",conditionVO);
        return Result.ok(albumService.albumBackList(conditionVO));
    }

    /**
     * 获取后台相册信息
     * @return
     */
    @ApiOperation(value = "获取后台相册信息")
    @GetMapping("admin/album/info")
    public Result<List<AlbumBackDTO>> albumInfo(){
        return Result.ok(albumService.getAlbumInfo());
    }
    /**
     * 上传图片(相册封面以及相册内的照片)
     * @param file
     * @return
     */
    @ApiOperation(value = "上传相册图片")
    @PostMapping("admin/album/photo")
    public Result<String> uploadAlbumImg(MultipartFile file){
        //log.info("上传图片：{}",file);
        return Result.ok(uploadStrategyContext.executeUploadStrategy(file,PHOTO.getPath()));
    }

    /**
     * 添加或更新相册
     * @param albumVO
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或更新相册")
    @PostMapping("admin/album")
    public Result saveOrUpdateAlbum(@Valid @RequestBody AlbumVO albumVO){
        //log.info("添加相册:{}",albumVO);
        albumService.saveOrUpdateAlbum(albumVO);
        return Result.ok();
    }

    /**
     * 通过id删除相册
     * @param id
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "通过id删除相册")
    @DeleteMapping("admin/album/{id}")
    public Result deleteAlbumById(@PathVariable("id") String id){
        //log.info("删除相册id:{}",id);
        albumService.deleteById(id);
        return Result.ok();
    }

    /**
     * 通过id获取相册
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id获取相册")
    @GetMapping("admin/album/{id}")
    public Result<AlbumBackDTO> getAlbumById(@PathVariable("id") String id){
        //log.info("相册id:{}",id);
        return Result.ok(albumService.getAlbumById(id));
    }


    /**
     * 获取首页相册列表
     * @return
     */
    @ApiOperation(value = "获取首页相册列表")
    @GetMapping("albums")
    public Result<List<AlbumDTO>> albumList(){
        return Result.ok(albumService.albumList());
    }


}
